<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */

namespace App\Content\Repositories;

use App\Content\Contracts\ArticleInterface;
use App\Content\Models\ArticleClassModel;
use App\Content\Models\ArticleModel;
use Illuminate\Support\Facades\Request;


/**
 * 用户新闻列表
 * Class DbGoodsRepository
 * @package App\Shop\Repositories
 */
class DbArticleRepository implements ArticleInterface
{
    /**
     * 新闻模型
     * @return ArticleModel
     */
    public function getArticleModel()
    {
        return app()->make(ArticleModel::class);
    }

    /**
     * 新闻分类模型
     * @return ArticleClassModel
     */
    public function getArticleClassModel()
    {
        return app()->make(ArticleClassModel::class);
    }

    /**
     * 获取文章列表
     * @param $where
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getArticleList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        return $this->getArticleModel()->newQuery()->where($where)->orderBy("create_time", "desc")->paginate($limit);
    }

    /**
     * 数据保存
     * @param $data
     */
    public function saveData($data)
    {
        if (empty($data['status'])) {
            $data['status'] = 0;
        }
        $data["uid"] = UID;
        return $this->getArticleModel()->newQuery()->updateOrCreate(["id" => $data['id']], $data);
    }

    /**
     * 获取文章信息
     * @param $id
     * @return mixed|void
     */
    public function getInfo($id)
    {
        return $this->getArticleModel()->newQuery()->where("id", $id)->first();
    }

    /**
     * 删除数据
     * @param $id
     * @return mixed
     */
    public function deleteData($where)
    {

        return $this->getArticleModel()->newQuery()->where($where)->delete();
    }
}